package com.think.admin.config;

import com.think.common.utils.JacksonUtils;
import com.think.common.utils.SysUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * <p>
 * redisson 配置类
 * </p>
 *
 * @author xwj
 * @date 2019/3/21 11:39
 */
@Configuration
public class RedissonConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private String port;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.database}")
    private int database;

    @Bean
    public RedissonClient redisson() throws IOException {
        Config config = Config.fromYAML(RedissonConfig.class.getClassLoader().getResource("redisson-config.yml"));
        if (SysUtils.isNotEmpty(password)) {
            config.useSingleServer().setAddress("redis://" + host + ":" + port).setPassword(password).setDatabase(database);
        }else{
            config.useSingleServer().setAddress("redis://" + host + ":" + port).setDatabase(database);
        }
        config.setCodec(new JsonJacksonCodec(JacksonUtils.getObjectMapper()));
        return Redisson.create(config);
    }
}
